package com.mediacloud.app.newsmodule.fragment.recommend;


import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.eventTrack.CDPTrackReportManager;
import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.model.component.ComponentItem;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.model.news.CMSFieldStyle;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.ShowSwitch;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.NewsListItemStyleAdaptor;
import com.mediacloud.app.newsmodule.adaptor.component.IExposure;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;

import java.util.List;

/**
 * 推荐阅读样式
 */

public class ComponentRecommendNewsView extends BaseComponentRelatedView implements AdapterView.OnItemClickListener {


    private ListView articleListView = null;
    private NewsListItemStyleAdaptor articlesAdapter = null;
    private CMSFieldStyle cmsFieldStyle = new CMSFieldStyle();
    private AppFactoryGlobalConfig.ServerAppConfigInfo serverAppConfigInfo;
    public ComponentRecommendNewsView(Context context) {
        super(context);
        serverAppConfigInfo = AppFactoryGlobalConfig.getAppServerConfigInfo(context);
    }


    @Override
    public int setLayoutRes() {
        return R.layout.component_related_recommendation;
    }

    @Override
    public void initOtherViews() {
        articleListView = Utility.findViewById(mRootView, R.id.articleListView);
    }


    @Override
    public void onDataSuccess(ArticleListData data) {
        List<ArticleItem> listArticles = data.articleList;
        if (listArticles == null || listArticles.size() == 0) {
            return;
        }
        for (ArticleItem item : listArticles) {
            item.setCmsCustomStyle(cmsFieldStyle);//去掉cms样式
           // item.mShowSwitch = showSwitch;
            item.mShowSwitch = new ShowSwitch();
            item.mShowSwitch.allowShowHitCount = serverAppConfigInfo.getShow_hit_count() == 1;
            item.mShowSwitch.allowShowInteractionCount = serverAppConfigInfo.getShow_interaction_count() == 1;
            item.mShowSwitch.allowShowPublishDate = serverAppConfigInfo.getShow_publish_date() == 1;
            item.mShowSwitch.allowShowSource = serverAppConfigInfo.getShow_source() == 1;
            item.mShowSwitch.allowShowComment = serverAppConfigInfo.is_comment == 1;
        }
        mRootView.setVisibility(VISIBLE);
        this.setVisibility(VISIBLE);
        final CatalogItem cat = new CatalogItem();
        articlesAdapter = new NewsListItemStyleAdaptor(getContext(), cat);
        articlesAdapter.isEssenceList = true;
        articlesAdapter.isNewsDetailRecommend = true;
        articlesAdapter.isRecommendList = true;
        articlesAdapter.setListContentData(listArticles);
        if (articleListView == null)
            articleListView = Utility.findViewById(mRootView, R.id.articleListView);

        articleListView.setDivider(null);
        articleListView.setAdapter(articlesAdapter);
        articlesAdapter.notifyDataSetChanged();
        articleListView.setOnItemClickListener(ComponentRecommendNewsView.this);
        for (int i = 0; i < listArticles.size(); i++) {
            CDPTrackReportManager.getInstance().NewsRPExpose(i,listArticles.get(i),"相关阅读");
        }
    }

    @Override
    public void onDataFault(Object data) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ArticleItem article = articlesAdapter.getItem(position);
        NewsItemClickUtils.OpenItemNewsHandle(getContext(), article.getType(), article, articlesAdapter.catalogItem);
        CDPTrackReportManager.getInstance().NewsRPClick(position,article,"相关阅读");
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (articlesAdapter != null) {
            articlesAdapter.destroy();
        }
    }


}